'''
Created on 15.12.2012

@author: felix
'''
from game.player.alpha_beta_player.alpha_beta_framework.heuristic.CompleteFieldHeuristic import CompleteFieldHeuristic
from game import GameLogic

class OpenDirectionsHeuristic(CompleteFieldHeuristic):
    '''
    classdocs
    '''


    def prepare(self):
        self.opened = 0.0
        self.divide = 0.0
        self.visited = set()
    
    def inLoopHeuristic(self, field, action, color, nextColor, depth, position):
        x,y = position
        if field.get(x,y) != color:
            return
        for direction in GameLogic.actions:
            oldCount = len(self.visited)
            rstones = GameLogic.countLen(field, position, direction, color, self.visited)
            stones = len(self.visited) - oldCount
            self.divide += stones
            oldCount = len(self.visited)
            GameLogic.numExpendableInDirection(field, position, direction, color, self.visited)
            self.opened += (len(self.visited) - oldCount) * rstones
    
    def afterLoopHeuristic(self):
        if self.divide == 0:
            self.divide = 1
        return self.opened/self.divide

